package com.catbad.controller;

import com.catbad.annotations.Auth;
import com.catbad.domain.Room;
import com.catbad.domain.User;
import com.catbad.services.RoomServices;
import com.catbad.services.UserServices;
import com.catbad.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PersonnelRoomController {
    @Autowired
    RoomServices roomServices;
    @Autowired
    UserServices userServices;
    /**
     * 进入放映厅管理页面,得到放映厅信息
     * @return
     */
    @Auth("com.catbad.personnel.room")
    @RequestMapping("personnelroom")
    public String personnelPlay(Integer pageNo, HttpServletRequest request, Model model){
        //获取放映厅页面信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        RoomVO roomVO = roomServices.getRoomListForRoomVO(pageNo,cid);
        model.addAttribute("roomVO",roomVO);
        return "personnel/room";
    }

    @Auth("com.catbad.personnel.room")
    @RequestMapping("saveRoom")
    @ResponseBody
    public Integer saveRoom(String roomName,HttpServletRequest request){
        int code = 0;
        //获得当前影院id
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        code = roomServices.saveRoom(roomName,cid);
        return code;
    }

    @Auth("com.catbad.personnel.room")
    @RequestMapping("deleteRoom")
    @ResponseBody
    public Integer deleteRoom(Long roomId){
        return roomServices.deleteRoomById(roomId);
    }

    @Auth("com.catbad.personnel.room")
    @RequestMapping("forEditRoom")
    @ResponseBody
    public Room forEditRoom(Long roomId){
        return roomServices.selectRoomById(roomId);
    }

    @Auth("com.catbad.personnel.room")
    @RequestMapping("editRoom")
    @ResponseBody
    public Integer editRoom(Long roomId,String roomName){
        return roomServices.updateRoomNameById(roomId,roomName);
    }
}
